package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.entity.SysConfigDo;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.system.service.ISysConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 参数配置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@RequiredArgsConstructor
@Service
public class SysConfigServiceImpl implements ISysConfigService {
    private final SysConfigMapper sysConfigMapper;

    /**
     * 查询参数配置
     */
    @Override
    public SysConfigDo queryById(Long configId) {
        return sysConfigMapper.selectById(configId);
    }

    /**
     * 查询参数配置列表
     */
    @Override
    public TableDataInfo<SysConfigDo> queryPageList(SysConfigDo entity, PageQuery pageQuery) {
        LambdaQueryWrapper<SysConfigDo> lqw = buildQueryWrapper(entity);
        Page<SysConfigDo> result = sysConfigMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询参数配置列表
     */
    @Override
    public List<SysConfigDo> queryList(SysConfigDo entity) {
        LambdaQueryWrapper<SysConfigDo> lqw = buildQueryWrapper(entity);
        return sysConfigMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysConfigDo> buildQueryWrapper(SysConfigDo entity) {
        Map<String, Object> params = entity.getParams();
        LambdaQueryWrapper<SysConfigDo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(entity.getConfigName()), SysConfigDo::getConfigName, entity.getConfigName());
        lqw.eq(StringUtils.isNotBlank(entity.getConfigKey()), SysConfigDo::getConfigKey, entity.getConfigKey());
        lqw.eq(StringUtils.isNotBlank(entity.getConfigValue()), SysConfigDo::getConfigValue, entity.getConfigValue());
        lqw.eq(StringUtils.isNotBlank(entity.getConfigType()), SysConfigDo::getConfigType, entity.getConfigType());
        return lqw;
    }

    /**
     * 新增参数配置
     */
    @Override
    public Boolean insert(SysConfigDo entity) {
        SysConfigDo add = BeanUtil.toBean(entity, SysConfigDo.class);
        validEntityBeforeSave(add);
        Boolean flag = sysConfigMapper.insert(add) > 0;
        if (flag) {
            entity.setConfigId(add.getConfigId());
        }
        return flag;
    }

    /**
     * 修改参数配置
     */
    @Override
    public Boolean updateById(SysConfigDo entity) {
        SysConfigDo update = BeanUtil.toBean(entity, SysConfigDo.class);
        validEntityBeforeSave(update);
        return sysConfigMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysConfigDo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除参数配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return sysConfigMapper.deleteBatchIds(ids) > 0;
    }
}
